package com.example.imoocmall.service.impl;

import com.example.imoocmall.common.Constant;
import com.example.imoocmall.exception.ImoocMallException;
import com.example.imoocmall.exception.ImoocMallExceptionEnum;
import com.example.imoocmall.model.dao.ProductMapper;
import com.example.imoocmall.model.pojo.Product;
import com.example.imoocmall.model.query.ProductListQuery;
import com.example.imoocmall.model.request.AddProductReq;
import com.example.imoocmall.model.request.ProductListReq;
import com.example.imoocmall.model.request.UpdateProductReq;
import com.example.imoocmall.service.ProductService;
import com.example.imoocmall.model.vo.CategoryVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CategoryServiceImpl categoryService;

    @Override
    public void add(AddProductReq addProductReq){
        Product product = new Product();
        BeanUtils.copyProperties(addProductReq,product);
        Product productOld = productMapper.selectByName(product.getName());
        if (productOld != null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = productMapper.insertSelective(product);
        if (count != 1){
            throw new ImoocMallException(ImoocMallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public void update(UpdateProductReq updateProductReq){
        Product product = new Product();
        BeanUtils.copyProperties(updateProductReq,product);
        //要判断商品名称不存在
        Product productOld = productMapper.selectByName(updateProductReq.getName());
        if (productOld != null && !product.getId().equals(productOld.getId())){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }

        int count = productMapper.updateByPrimaryKeySelective(product);
        if (count != 1){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }


    @Override
    public void delete(Integer id){
        int count = productMapper.deleteByPrimaryKey(id);
        if (count!=1){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }



    @Override
    public void batchUpdateSellStatus(Integer[] ids, Integer sellStatus){
        int count = productMapper.batchUpdateSellStatus(ids,sellStatus);
        if (count != ids.length){
            throw new ImoocMallException(ImoocMallExceptionEnum.SELL_STATUS_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productMapper.selectListForAdmin();
        PageInfo pageInfo = new PageInfo(products);
        return pageInfo;
    }


    @Override
    public Product detail(Integer id){
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    @Override
    public PageInfo search(ProductListReq productListReq){

        ProductListQuery productListQuery = new ProductListQuery();
        //关键字
        if (!StringUtils.isEmptyOrWhitespaceOnly(productListReq.getKeyword())){
            productListQuery.setKeyword(productListReq.getKeyword());
        }
        //类型
        if (productListReq.getCategoryId()!=null){
            List<CategoryVo> categoryVos = categoryService.listCategoryForCustomer(productListReq.getCategoryId());
            List<Integer> categroyIds = new ArrayList<>();
            categroyIds.add(productListReq.getCategoryId());
            getCategoryIds(categoryVos,categroyIds);
            productListQuery.setCategoryIds(categroyIds);

        }
        //排序
        if (Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(productListReq.getOrderBy())){
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize(),productListReq.getOrderBy());
        }else {
            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize());
        }

        List<Product> products = productMapper.selectList(productListQuery);
        return new PageInfo(products);


//        //构建query对象，把通过什么来筛选的统计起来
//        ProductListQuery productListQuery = new ProductListQuery();
//        //通过关键字来找
//        if (!StringUtils.isEmptyOrWhitespaceOnly(productListReq.getKeyword())){
//            String keyword = new StringBuilder().append("%").append(productListReq.getKeyword()).append("%").toString();
//            productListQuery.setKeyword(keyword);
//        }
//        //目录处理,查目录的话需要把该目录和下级目录都查询出来
//        if (productListReq.getCategoryId()!=null){
//            List<CategoryVo> categoryVos = categoryService.listCategoryForCustomer(productListReq.getCategoryId());
//            ArrayList<Integer> categoryIds = new ArrayList<>();
//            categoryIds.add(productListReq.getCategoryId());
//            getCategoryIds( categoryVos, categoryIds);
//            productListQuery.setCategoryIds(categoryIds);
//        }
//        //排序处理
//        String orderBy = productListReq.getOrderBy();
//        if (Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
//            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize(),orderBy);
//        }else {
//            PageHelper.startPage(productListReq.getPageNum(),productListReq.getPageSize());
//        }
//
//        List<Product> products = productMapper.selectList(productListQuery);
//        PageInfo pageInfo = new PageInfo(products);
//        return pageInfo;
    }
    
    
    private void getCategoryIds(List<CategoryVo> categoryVos,List<Integer> categoryIds){
        for (int i = 0; i < categoryVos.size(); i++) {
            CategoryVo categoryVo =  categoryVos.get(i);
            categoryIds.add(categoryVo.getId());
            getCategoryIds(categoryVo.getChildCategory(),categoryIds);

        }










//        for (int i = 0; i < categoryVos.size(); i++) {
//            CategoryVo categoryVo =  categoryVos.get(i);
//            if (categoryVo != null){
//                categoryIds.add(categoryVo.getId());
//                getCategoryIds(categoryVo.getChildCategory(),categoryIds);
//            }
//        }
    }








}
